/*
 * 描述
 * Created by: litangyu
 * 包含类名列表
 * 版本信息，版本号 v1.0
 * 创建日期 16/8/30
 * 版权声明
*/
package com.foton.logistic.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.foton.logistic.R;
import com.foton.logistic.activity.LoginActivity;
import com.foton.logistic.activity.ModifyPasswordActivity;
import com.foton.logistic.activity.ModifyTelActivity;
import com.foton.logistic.activity.PersonalInfoActivity;
import com.foton.logistic.activity.ShopListActivity;
import com.foton.logistic.activity.set.AboutActivity;
import com.foton.logistic.base.BaseApplication;
import com.foton.logistic.base.BaseFragment;
import com.foton.logistic.listener.IOnDialogListener;
import com.foton.logistic.listener.IOnResultListener;
import com.foton.logistic.listener.IOnTryClickListener;
import com.foton.logistic.model.user.UserResultEntity;
import com.foton.logistic.task.DispatchTask;
import com.foton.logistic.task.ShowLoadTask;
import com.foton.logistic.task.TaskConstant;
import com.foton.logistic.util.BaseConstant;
import com.foton.logistic.util.IntentUtil;
import com.foton.logistic.util.SharedUtil;
import com.foton.logistic.util.VersionCheckUtil;
import com.foton.logistic.util.net.HttpUtil;
import com.foton.logistic.util.tool.DataCleanManager;
import com.foton.logistic.view.dialog.DialogUtil;

import java.util.Map;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Optional;

/**
 * 描述
 * 个人中心
 * Created by: litangyu<br/>
 * Created on: 16/8/30 上午9:19
 */
public class PersonalCenterFragment extends BaseFragment {

    private View rootView;
  // @InjectView(R.id.ft_ui_personal_center_tv_username)
    public TextView mUserName;

//    private UserModelEntity data;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        setTaskTag(getClass().getSimpleName());
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.ft_fragment_personal_center, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout
                    .LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            rootView.setLayoutParams(params);
            ButterKnife.inject(this, rootView);
            init();
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void init() {
        setTitleTextVisibility(View.VISIBLE);
        setTitleText(getString(R.string.personal_center));
        mUserName= (TextView) rootView.findViewById(R.id.ft_ui_personal_center_tv_username);
        request();
    }

    @Override
    public void onResume() {
        super.onResume();
        //request();
    }

    /**
     * 发起请求
     */
    private void request() {
        Map<String, Object> argMap = HttpUtil.getEncryMap(getActivity());
        ShowLoadTask task = new ShowLoadTask(getActivity(), taskTag, containLayout, loadLayout,
                "", false, retryListener, BaseConstant.GET_DRIVER_INFO,
                argMap, TaskConstant.POST);
        task.setParseClass(UserResultEntity.class);
        task.setiOnResultListener(new IOnResultListener() {

            @Override
            public void onOK(DispatchTask task) {
                if (task.resultEntity instanceof UserResultEntity) {
                    UserResultEntity entity = (UserResultEntity) task.resultEntity;
                    if (null != entity.getData()) {
                        mUserName.setText(entity.getData().getDriverName());
                        SharedUtil.saveUserInfo(getActivity(), entity.getData());
                    }
                }
            }

            @Override
            public void onError(DispatchTask task) {
            }

            @Override
            public void onDone(DispatchTask task) {

            }
        });
        task.execute();
    }

    @Override
    protected void lazyLoad() {

    }

    @Optional
    @OnClick({R.id.ft_ui_personal_center_personal_info_layout,
            R.id.ft_ui_personal_center_modify_password_layout,
            R.id.ft_ui_personal_center_about_us_layout,
            R.id.ft_ui_personal_center_log_off,
            R.id.ft_ui_personal_center_check_version_layout,R.id.ft_ui_privacy_layout})
    public void onClick(View view) {
        Intent intent;
//        Bundle bundle;
        switch (view.getId()) {
            case R.id.ft_ui_personal_center_personal_info_layout:
                //个人信息
                intent = new Intent(getActivity(), PersonalInfoActivity.class);
//                bundle = new Bundle();
//                bundle.putSerializable("info",data);
//                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.ft_ui_personal_center_modify_password_layout:
                //修改密码
                intent = new Intent(getActivity(), ModifyPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.ft_ui_personal_center_about_us_layout:
                //关于我们
                intent = new Intent(getActivity(), AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.ft_ui_personal_center_log_off:
                //退出登录
                showLogOffDialog(view);
                break;
            case R.id.ft_ui_personal_center_check_version_layout:
                //检查更新
                VersionCheckUtil versionCheckUtils = new VersionCheckUtil(getActivity(), titleText, taskTag);
                versionCheckUtils.setShowToast(true);
                versionCheckUtils.checkVersion(true);
                break;
            case R.id.ft_ui_privacy_layout:
                IntentUtil.intent2PrivacyRuler(getActivity());
                /*intent = new Intent(getActivity(), PrivacyActivity.class);
                startActivity(intent);*/
                //IntentUtil.intent2Agree(RegisterActivity.this);
                break;
        }
    }


    /**
     * 退出登录
     *
     * @param view
     */
    private void showLogOffDialog(View view) {
        DialogUtil dialogUtil = new DialogUtil(getActivity());
        dialogUtil.setTitle(getString(R.string.dialog_log_off));
        dialogUtil.setDismissKeyback(true);
        dialogUtil.setDismissOutside(true);
        dialogUtil.showTipDialog(view, " ");
        dialogUtil.setiOnDialogListener(new IOnDialogListener() {
            @Override
            public void onConfirm() {
                SharedUtil.saveRetryTime(getActivity(), 1800000);
                SharedUtil.saveUserId(getActivity(), "");
                SharedUtil.saveToken(getActivity(), "");

                BaseApplication.self().stopOut(true);
//                BaseApplication.self().stopLocation();

                screenManager.closeAll();
                DataCleanManager.cleanCustomCache(getActivity(), BaseConstant
                        .IMAGETAMPPATH);

                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onOther() {

            }
        });
    }

    IOnTryClickListener retryListener = new IOnTryClickListener() {
        @Override
        public void onTry() {
            request();
        }

        @Override
        public void onOption() {

        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({R.id.ft_ui_personal_center_modify_tel_layout, R.id.ft_ui_personal_center_shop_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ft_ui_personal_center_modify_tel_layout:
                Intent intent = new Intent(getActivity(), ModifyTelActivity.class);
                startActivity(intent);
                break;
            case R.id.ft_ui_personal_center_shop_layout:
                Intent intent1 = new Intent(getActivity(), ShopListActivity.class);
                startActivity(intent1);
                break;
        }
    }


}
